﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SPCFMedicineInformer.DocumentTypes;
using Vega.USiteBuilder;
using SPCFMedicineInformer.Members;
using Examine;
using Examine.SearchCriteria;
using Examine.LuceneEngine.SearchCriteria;
namespace SPCFMedicineInformer.Masterpages
{
    public partial class FrontPageTemplate : TemplateBase<FrontPage>
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var User = HttpContext.Current.User.Identity;
            var member = MemberHelper.GetMemberFromEmail(User.Name) as WebUser;
            //plcSlider.Visible = !User.IsAuthenticated;
            //plcLoginSide.Visible = User.IsAuthenticated;
            //plcFrontPageContent.Visible = !User.IsAuthenticated;
            if (member != null)
                litUserName.Text = member.Fistname;

            string query = Request.QueryString["q"] != null ? Request.QueryString["q"].ToString().ToLower() : "a";
            Search(query);

        }

        #region ExamineIndexer
        void Search(string searchTerm = "a")
        {
            ISearchCriteria filter;
            var criteria = ExamineManager.Instance.SearchProviderCollection["MedicineSearcher"].CreateSearchCriteria();
            
            filter = criteria.Field("medicineName", searchTerm.Fuzzy()).Compile();

            var results = ExamineManager.Instance.SearchProviderCollection["MedicineSearcher"].Search(filter)
                   .Select(MedicineDTO.GetDTO)
                   .Where(x => x != null);

            Repeater1.DataSource = results.ToList();
            Repeater1.DataBind();
        }
        public class MedicineDTO
        {
            public string Id { get; set; }
            public string NodeName { get; set; }
            public string CategoryId { get; set; }
            public string GenericId { get; set; }
            public string MedicineName { get; set; }
            public string NodeTypeAlias { get; set; }

            public static MedicineDTO GetDTO(SearchResult result)
            {
                string id, nodeName, categoryId, genericId, medicineName, nodeTypeAlias;

                result.Fields.TryGetValue("id", out id);
                result.Fields.TryGetValue("nodeName", out nodeName);
                result.Fields.TryGetValue("categoryId", out categoryId);
                result.Fields.TryGetValue("genericId", out genericId);
                result.Fields.TryGetValue("medicineName", out medicineName);
                result.Fields.TryGetValue("nodeTypeAlias", out nodeTypeAlias);

                return new MedicineDTO
                {
                    Id = id,
                    NodeName = nodeName,
                    CategoryId = categoryId,
                    GenericId = genericId,
                    MedicineName = medicineName,
                    NodeTypeAlias = nodeTypeAlias
                };
            }
        }
        #endregion

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                MedicineDTO dataItem = (MedicineDTO)e.Item.DataItem;
                LinkButton linkBtnMedicine = (LinkButton)e.Item.FindControl("linkBtnMedicine");
                linkBtnMedicine.Text = dataItem.MedicineName;
            }
        }
    }
}